package com.itheima.Service.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Employee;
import com.itheima.dao.EmployeeDao;
import com.itheima.Service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

@Transactional
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    EmployeeDao employeeDao;

    @Override
    public int addEmployee(Employee employee) {
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        int row = employeeDao.insert(employee);
        return row;
    }

    @Override
    public int updateEmployee(Employee employee) {

        return employeeDao.updateById(employee);
    }

    @Override
    public Employee selectById(long id) {
        return employeeDao.selectById(id);
    }

    @Override
    public IPage<Employee> findPage(int page, int pageSize, String name) {
        Page<Employee> employeePage = new Page<>();
        employeePage.setCurrent(page);
        employeePage.setSize(pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Employee::getName, name);
        return employeeDao.selectPage(employeePage, lqw);
    }

    @Override
    public Employee findByEmployee(Employee employee) {
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        lqw.eq(Employee::getPassword, password);
        Employee employee1 = employeeDao.selectOne(lqw);
        return employee1;
    }
}
